package cn.wolfcode.controller;

import cn.wolfcode.annotation.RequiredPermission;
import cn.wolfcode.domain.Department;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.result.JsonResult;
import cn.wolfcode.service.IDepartmentService;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    //@RequiresPermissions(value={"department:list","部门页面"},logical = Logical.OR)
    /*@RequestMapping("/list")
    public String list(@ModelAttribute("qo") QueryObject qo, Model model) {
        PageInfo<Department> pageInfo = departmentService.list();
        model.addAttribute("result", pageInfo);
        return "department/list";
    }*/

    //新增或编辑
    @RequestMapping("/saveOrUpdate")
    public String insert(Department department) {
        //通过获取参数对象中的ID 来判断是新增还是编辑
        if (department.getId()==null){
            departmentService.save(department);
        }else{
            departmentService.saveOrUpdate(department);
        }
        return "redirect:/department/list";
    }

    //删除
    //@RequiredPermission(name = "部门删除",experssion = "department:delete")
    @RequiresPermissions(value={"department:delete","部门删除"},logical = Logical.OR)
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        departmentService.removeById(id);
        return JsonResult.success(null);
    }

   /* @RequestMapping("/export")
    public void export(HttpServletResponse resp) throws IOException {
        //提交一个数据保存到xls的请求  JAVA 操作 XLS 文件
        //在响应头信息中要设定保存的文件名 保存类型
        resp.setHeader("Content-Disposition","attachment;filename=department.xls");
        //需要操作XLS   WorkBook
        //获取数据
        Workbook workbook = departmentService.getWorkbook();
        //保存 生成文件
        workbook.write(resp.getOutputStream());
    }*/
}
